package com.foreverwin.mes.production.controller;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.meapi.model.Resrce;
import com.foreverwin.mes.production.model.ClockLog;
import com.foreverwin.mes.production.model.Station;
import com.foreverwin.mes.production.service.ClockLogService;
import com.foreverwin.mes.production.service.StationResourceService;
import com.foreverwin.mes.production.service.StationService;
import com.foreverwin.mes.production.service.WorkCenterShiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author Syngna
 * @since 2019-01-03
 */
@RestController
@RequestMapping("/clockLogs")
public class ClockLogController {

    @Autowired
    private StationService stationService;
    @Autowired
    private ClockLogService clockLogService;
    @Autowired
    private WorkCenterShiftService workCenterShiftService;
    @Autowired
    private StationResourceService stationResourceService;

    /**
     * 岗位上岗-检索
     * 1.岗位基本信息
     * 2.岗位下分配的设备
     * 3.岗位在岗人员
     * 4.岗位预约人员
     *
     * @param station
     * @return
     */
    @ResponseBody
    @GetMapping("/getClockLog")
    public AjaxResult getClockLog(String station) {

        Date nowDate = new Date();
        String site = CommonMethods.getSite();
        Map<String, Object> result = new HashMap<String, Object>();
        try {

            //查询岗位信息
            Station stationInfo = stationService.getStation(site, station);
            if (stationInfo == null) {
                throw BusinessException.build("岗位：" + station + "不存在");
            }
            if (!"Y".equals(stationInfo.getStatus())) {
                throw BusinessException.build("岗位：" + station + "状态不可用");
            }
            String stationBo = stationInfo.getHandle();

            //查询岗位分配的设备
            List<Resrce> resourceList = stationResourceService.getResourceList(stationBo);
            if (resourceList == null || resourceList.size() < 1) {
                throw BusinessException.build("当前岗位:" + station + "未分配设备！");
            }
            String resource = resourceList.get(0).getResrce();

            //查询在岗人员
            List<ClockLog> clockInEmployeeList = clockLogService.getStationClockInEmployee(site, station, nowDate);


            //查询当前工作中心维护班次
            Map<String, Object> map = workCenterShiftService.getAppointOrCurrentShiftOfResource(site, resource, nowDate);
            if (map != null) {
                result.put("IS_APPOINTMENT", map.get("isAppointment"));
            }
            result.put("STATION", stationInfo);
            result.put("WORK_LIST", clockInEmployeeList);
            result.put("RESOURCE_LIST", resourceList);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 根据资源查询岗位打卡人员
     *
     * @return
     */
    @GetMapping("/getResourceEmployee")
    public AjaxResult getResourceEmployee(String resource) {

        String site = CommonMethods.getSite();
        List<ClockLog> result = new ArrayList<>();
        try {

            result = clockLogService.getResourceClockInEmployee(site, resource);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 人员离岗
     *
     * @param jsonObject
     * @return
     */
    @ResponseBody
    //@Secured(loginCheck = true)
    @RequestMapping(value = "doClockOut", method = RequestMethod.POST)
    public AjaxResult doCheckOut(@RequestBody JSONObject jsonObject) {

        String site = CommonMethods.getSite();
        try {

            String shift = jsonObject.getString("shift");
            String station = jsonObject.getString("station");
            String employee = jsonObject.getString("employee");
            if (StringUtils.isBlank(shift)) {
                throw BusinessException.build("班次不能为空！");
            } else if (StringUtils.isBlank(station)) {
                throw BusinessException.build("岗位不能为空！");
            } else if (StringUtils.isBlank(employee)) {
                throw BusinessException.build("人员不能为空！");
            }

            clockLogService.clockOut(site, station, shift, employee);

        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success("离岗成功！");
    }

    /***
     * 人员上岗
     * @param jsonObject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "doClockIn", method = RequestMethod.POST)
    public AjaxResult doClockIn(@RequestBody JSONObject jsonObject) {

        String site = CommonMethods.getSite();
        try {

            Date nowDate = new Date();

            String station = jsonObject.getString("station");
            String employee = jsonObject.getString("employee");

            if (StringUtils.isBlank(station)) {
                throw BusinessException.build("岗位不能为空！");
            } else if (StringUtils.isBlank(employee)) {
                throw BusinessException.build("人员不能为空！");
            }

            clockLogService.clockIn(site, station, employee, nowDate);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success("上岗成功！");
    }

}